package com.ffcs.ffxtc.interceptor;

import cn.hutool.core.util.ObjectUtil;
import com.ffcs.utils.JWTUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

/**
 * 自定义拦截器类
 *
 * @author zhangzhengxing
 */
public class GlobalHandlerInterceptor implements HandlerInterceptor {

    /**
     * 访问控制器方法前执行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
//        if (ObjectUtil.isEmpty(request.getHeader("Authorization"))) {
//            throw new RuntimeException("token已失效!");
//        }
//
//        String token = URLDecoder.decode(request.getHeader("Authorization"), StandardCharsets.UTF_8);
//        token = token.split(" ")[1];
//
//        JWTUtils.verify(token);
        return true;
    }

    /**
     * 访问控制器方法后执行
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
//    System.out.println(new Date() + "--postHandle:" + request.getRequestURL());
    }
    
    /**
     * postHandle方法执行完成后执行，一般用于释放资源
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        //  System.out.println(new Date() + "--afterCompletion:" + request.getRequestURL());
    }
}